package com.xzz.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@Configuration
@EnableWebSecurity  //开启Security
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled= true) //开启方法授权
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * UserDetailsService，保存全部登入用户信息
     * PasswordEncoder，密码编码器，点击登录的时候，Security会拦截登入人信息，用过密码编码器解析密码，与登入用户信息比较
     * 比较成功之后，会把登入人信息和权限，存储在SecurityContext中，也可以存在客户端TOKEN
     *
     * 通过方法授权，方法授权中的权限别名，与存储在SecurityContext或客户端中权限匹配
     *
     * Oauth2会使用认证管理器，去委派给Security的认证服务完成认证，登录成功，就包含了用户信息和权限
     */

     //提供用户信息，这里没有从数据库查询用户信息，在内存中模拟
   /* @Bean
    public UserDetailsService userDetailsService(){
        InMemoryUserDetailsManager inMemoryUserDetailsManager =
        new InMemoryUserDetailsManager();
        inMemoryUserDetailsManager.createUser(User.withUsername("zs").password("123").authorities("admin").build());
        return inMemoryUserDetailsManager;
    }*/

    //配置认证管理器，授权模式为“poassword”时会用到
    //Oauth2会使用认证管理器，去委派给Security的认证服务完成认证，登录成功，就包含了用户信息和权限
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    //密码编码器：通过BCryptPasswordEncoder进行解密，跟MD5加密一样
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
    
    //授权规则配置
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()                                //授权配置
                .antMatchers("/login/common").permitAll()  //登录路径放行
                .anyRequest().authenticated()                   //其他路径都要认证之后才能访问
                .and().formLogin()                              //允许表单登录
                .successForwardUrl("/loginSuccess")             // 设置登陆成功页
                .and().logout().permitAll()                    //登出路径放行 /logout
                .and().csrf().disable();                        //关闭跨域伪造检查
    }

}